<?php
namespace Backend\Controllers\Setting\Sys;

use Backend\Controllers\BaseController;
use Backend\Models\Sys\User;

class UserController extends BaseController
{

    public function initialize()
    {
        parent::initialize();
    }

    public function getsAction()
    {
        if ($this->user == false) {
            return $this->json([
                'code' => 501,
                'msg'  => '请登录后再试~'
            ]);
        }
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('pageSize')
        ];
        if ($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的页数！']);
        }
        if ($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的每页的条数！']);
        }
        $data['size'] = intval($data['size']);
        $data['page'] = intval($data['page']);
        $offset       = intval(($data['page'] - 1) * $data['size']);
        $users        = (new User())->gets($data['size'], $offset);
        if ($users === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        return $this->json([
            'code'  => 200,
            'users' => $users
        ]);
    }

    public function getCountAction()
    {
        $cnt = (new User())->getCount();
        if ($cnt === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        return $this->json([
            'code'  => 200,
            'count' => $cnt
        ]);
    }

    public function addAction()
    {
        if ($this->user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '请登录后再试～'
            ]);
        }
        $data = [
            'name'      => trim($this->request->getPost('name')),
            'account'   => trim($this->request->getPost('account')),
            'password'  => trim($this->request->getPost('password')),
            'password2' => trim($this->request->getPost('password2')),
            'role_id'   => $this->request->getPost('role_id'),
            'status'    => $this->request->getPost('status'),
            'email'     => trim($this->request->getPost('email')),
            'mobile'    => trim($this->request->getPost('mobile')),
            'tel'       => trim($this->request->getPost('tel'))
        ];
        if ($data['name'] == '') {
            return $this->json([
                'code' => 301,
                'msg'  => '请填写姓名～'
            ]);
        }
        if ($data['account'] == '') {
            return $this->json([
                'code' => 302,
                'msg'  => '请填写账户名～'
            ]);
        }
        if ($data['password'] == '') {
            return $this->json([
                'code' => 303,
                'msg'  => '请填写密码～'
            ]);
        }
        if (mb_strlen($data['password'], 'utf-8') < 6) {
            return $this->json([
                'code' => 304,
                'msg'  => '密码不能少于6位～'
            ]);
        }
        if ($data['password2'] == '') {
            return $this->json([
                'code' => 305,
                'msg'  => '请填写重复密码～'
            ]);
        }
        if ($data['password'] != $data['password2']) {
            return $this->json(['code' => 306, 'msg' => '两次密码不一致，请重新填写～']);
        }
        if ($data['role_id'] == '' || !is_numeric($data['role_id'])) {
            return $this->json(['code' => 307, 'msg' => '请选择用户角色～']);
        }
        if ($data['status'] == '' || !is_numeric($data['status'])) {
            return $this->json(['code' => 309, 'msg' => '请选择状态～']);
        }
        $data['password']       = md5($data['password']);
        $data['status']         = intval($data['status']);
        $data['role_id']        = intval($data['role_id']);
        $data['create_at']      = date('Y-m-d H:i:s');
        $data['last_update_at'] = date('Y-m-d H:i:s');
        $user                   = new User();
        $checker                = $user->getByAccount($data['account']);
        if (is_array($checker) && count($checker) > 0) {
            return $this->json([
                'code' => 308,
                'msg'  => '已经添加了该用户，请重新添加～'
            ]);
        }
        $ret = $user->add($data);
        if ($ret) {
            return $this->json([
                'code' => 200,
                'msg'  => '新增用户成功～'
            ]);
        } else {
            return $this->json([
                'code' => 309,
                'msg'  => '添加失败，请稍候再试～'
            ]);
        }
    }

    public function getAction()
    {
        $uid  = $this->request->getPost('uid');
        $user = new User();
        $ret  = $user->get($uid);
        if ($ret === false) {
            return $this->json(['code' => 300, 'msg' => '用户不存在～']);
        }
        return $this->json(['code' => 200, 'user' => $ret]);
    }

    public function editAction()
    {
        if ($this->user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '请登录后再试～'
            ]);
        }
        $data = [
            'uid'     => $this->request->getPost('uid'),
            'role_id' => $this->request->getPost('role_id')
        ];
        if ($data['role_id'] == '' || !is_numeric($data['role_id']) || $data['role_id'] == 0) {
            return $this->json([
                'code' => 301,
                'msg'  => '请选择用户角色～'
            ]);
        }
        $user = new User();
        if ($user->edit($data) === false) {
            return $this->json([
                'code' => 302,
                'msg'  => '编辑失败，请稍候再试～'
            ]);
        }
        return $this->json([
            'code' => 200,
            'msg'  => '编辑成功～'
        ]);
    }

    public function changeStatusAction()
    {
        if ($this->user == false) {
            return $this->json([
                'code' => 500,
                'msg'  => '您还未登录～'
            ]);
        }
        $uid  = $this->request->getPost('uid');
        $user = new User();
        if ($user->changeStatus($uid) === false) {
            return $this->json([
                'code' => 301,
                'msg'  => '修改失败，系统错误～'
            ]);
        }
        return $this->json(['code' => 200, 'msg' => '修改成功～']);
    }
}
